package com.ucap.bbs.controller.filter;

import java.io.IOException;
import java.util.Properties;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.velocity.app.Velocity;

import com.ucap.bbs.util.InitConfig;
import com.ucap.bbs.util.template.BaseTemplate;

public class DefaultFilter implements Filter {

	private Logger logger = Logger.getLogger(this.getClass());
	private static String REAL_PATH;
	public static String getRealPath(){
	    return REAL_PATH;
	}
//	private static long LAST_TIME;
//	private static long INDEX_UPDATETIME = 60*60*1000;
//	private static String INDEX_UPDATE_APP = "INDEX_UPDATE_APP";

	public void destroy() {
		logger.debug("DefaultFilter 过滤器已销毁...");
	}

	public void doFilter(ServletRequest sRequest, ServletResponse sResponse,
			FilterChain filterChain) throws IOException, ServletException {
		HttpServletResponse response = (HttpServletResponse) sResponse;
		HttpServletRequest request = (HttpServletRequest) sRequest;
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("UTF-8");
		System.out.println(request.getRequestURI());
		request.setAttribute("request_url", request.getRequestURL());
		filterChain.doFilter(request, response);
	}

	public void init(FilterConfig config) throws ServletException {
	    //初始化配置
		new InitConfig();
		//模板框架配置
		try{
            Properties prop = new Properties();
            REAL_PATH = config.getServletContext().getRealPath("/");
            System.out.println(REAL_PATH);
            prop.put("runtime.log", config.getServletContext().getRealPath("/WEB-INF/log/velocity.log"));
            prop.put("file.resource.loader.path", config.getServletContext().getRealPath("/WEB-INF/jsp"));
            prop.put("input.encoding", "UTF-8");
            prop.put("output.encoding", "UTF-8");
            Velocity.init(prop);
        } catch (Exception e) {
            e.printStackTrace();
        }
        new BaseTemplate();
	}
}

